<?php
session_start();
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Service/ArticleService.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Service/CustomerService.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Entity/Domain/OrderDetailsDomain.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Entity/Info/OrderDetail.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/Context/BaseContext.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../model/ServiceResult/ServiceResult.php';
$articleService = new ArticleService();

if(isset($_GET['code']) && isset($_GET['quantity']) && $_GET['quantity'] >0 && is_numeric($_GET['quantity']))
{
?>
	
	
<?php 
	
	$context = new BaseContext();
	$result = new ServiceResult();
	$oid = $_GET['code'];
	$quantity =  $_GET['quantity'];
	$currency = $_GET['currency'];
	$customerID = $_GET['customerID'];
	$flag = false;
	if(isset($_SESSION['orderDetail']))
	{
		$orderDetailArr = $_SESSION['orderDetail'];
		$i = 0;
		foreach ($orderDetailArr as $orderDetail)
        {
        	$orderDetail = unserialize($orderDetail);
        	if($orderDetail->getArticle()->getArticleDomain()->getCode() == $oid)
        	{
        		
        		$curQuantity = $orderDetail->getOrderdetailsDomain()->getQuantity();
        		$orderDetail->getOrderdetailsDomain()->setQuantity($curQuantity + $quantity);
        		
        		$newQuantity = $orderDetail->getOrderdetailsDomain()->getQuantity();
        		
        		$curPrice = $orderDetail->getOrderdetailsDomain()->getPrice();
        		$curPriceFact = $orderDetail->getOrderdetailsDomain()->getPriceFact();
        		
        		$orderDetail->getOrderdetailsDomain()->setAmount(($newQuantity * $curPrice));
        		$orderDetail->getOrderdetailsDomain()->setAmountFact(($newQuantity * $curPriceFact));
        		$_SESSION['orderDetail'][$i] = serialize($orderDetail);
        		$flag = true;
        		break;
        	}
        	$i++;
        }
        
	}
	$isExistArticle = true;
	
	if(!$flag)
	{
		
		$article = $articleService->getArticleByCodeByCustomer($context, $result, $oid, $customerID);
		
		if(!isset($article))
		{
			$isExistArticle = false;
		}
		else 
		{
			if($currency == 'USD')
			{
				$price = $article->getArticleDomain()->getPriceUSD();
				$priceFact = $article->getArticleDomain()->getInPriceUSD();
			}
			else 
			{
				$price = $article->getArticleDomain()->getPriceEUR();
				$priceFact = $article->getArticleDomain()->getInPriceEUR();
			}
			
			$data = array(
									'articleID' => $article->getArticleDomain()->getID(),
									'code' => $article->getArticleDomain()->getCode(),
									'quantity' => $quantity,
									'currency' => $currency,
									'price' => $price,
									'priceFact' => $priceFact,
									'amount' => ($price * $quantity),
									'amountFact' => ($priceFact * $quantity),
									'factoryID' => $article->getArticleDomain()->getFactoryID(),
									'subOrderNumber' => ''
								);
			$orderDetailsDomain = new OrderdetailsDomain($data);		
			$orderDetail = new OrderDetail($orderDetailsDomain);
			$orderDetail->setArticle($article);
			if(!isset($_SESSION['orderDetail']))
			{
				$_SESSION['orderDetail'] = array();
				array_push($_SESSION['orderDetail'], serialize($orderDetail));
			}
			else
			{
				
				array_push($_SESSION['orderDetail'], serialize($orderDetail));
			}
		}
	}
	if($isExistArticle)
	{
		$res = array();
		$str = '';
		$customerServices = new CustomerServices();
		$customer = $customerServices->getCustomerByOid($context, $result, $customerID);
		$totalAmount = 0;
		$totalBartTax = 0;
		$orderDetailArr = $_SESSION['orderDetail'];
		$strCurrency;
		$count = 1;
		$totalCbm = 0;
		$totalQuantity = 0;
		$curComm = $customer->getCustomerDomain()->getComm();
		$curVAT = 0;
		$totalAmountFact = 0;

		
		if(isset($_SESSION['VAT']))
		{
			$curVAT = $_SESSION['VAT'];
		}
		
		foreach ($orderDetailArr as $orderDetail)
	    {
	    	$orderDetail = unserialize($orderDetail);
	    	$str .= '<tr class="gradeX odd">
				<td>' . $count . '</td>
				<td>' . $orderDetail->getArticle()->getArticleDomain()->getCode() .'</td>
				<td>' . $orderDetail->getArticle()->getArticleDomain()->getDescription() .'</td>
				<td>' . $orderDetail->getArticle()->getLineDomain()->getName() .'</td>
				<td>' . $orderDetail->getArticle()->getColourDomain()->getDescription() .'</td>
				<td align="center">' . $orderDetail->getArticle()->getArticleDomain()->getWidth() .'</td>
				<td align="center">' . $orderDetail->getArticle()->getArticleDomain()->getHeight() .'</td>
				<td align="center">' . $orderDetail->getArticle()->getArticleDomain()->getDepth() .'</td>
				<td align="right">' . ($orderDetail->getArticle()->getArticleDomain()->getCbm() * $orderDetail->getOrderdetailsDomain()->getQuantity()) .'</td>
				<td align="right">' . $orderDetail->getOrderdetailsDomain()->getQuantity()  .'</td>
				<td align="right">' . $orderDetail->getOrderdetailsDomain()->getPrice() .'</td>
				<td align="center">'. $orderDetail->getOrderdetailsDomain()->getCurrency() .'</td>
				<td align="right">' . $orderDetail->getOrderdetailsDomain()->getAmount() .'</td>
				<td align="center">
					<a class="viewImage" href="' . $orderDetail->getArticle()->getArticleDomain()->getCode() .'" >View Pic</a> |
					<button type="button" class="btn btn-small" name="btn_consignee" onclick="removeArticle(' . $orderDetail->getArticle()->getArticleDomain()->getCode() .')" ><span></span>Delete</button>
					<div id="imagelist_' . $orderDetail->getArticle()->getArticleDomain()->getCode() .'">
					</div>				
				</td>
			</tr>';
	    	$strCurrency = $orderDetail->getOrderdetailsDomain()->getCurrency();
	    	$totalCbm = $totalCbm + ($orderDetail->getArticle()->getArticleDomain()->getCbm() * $orderDetail->getOrderdetailsDomain()->getQuantity());
	    	$totalQuantity = $totalQuantity + $orderDetail->getOrderdetailsDomain()->getQuantity();
	    	$totalAmount = $totalAmount + $orderDetail->getOrderdetailsDomain()->getAmount();
	    	$totalAmountFact = $totalAmountFact + $orderDetail->getOrderdetailsDomain()->getAmountFact();
	    	$count++;
	    }
	    $totalBartTax = $totalAmount * ($curComm/100);
		$totalAmountBeforeTax = $totalAmount + $totalBartTax;
		$totalAmountAfterTax = $totalAmountBeforeTax + ($totalAmountBeforeTax * ($curVAT/100));
		$totalAmountAfterTaxFact = $totalAmountFact + ($totalAmountFact * ($curVAT/100));
	    $res['tbody'] = $str;
	    
	    $strTfoot = '<tr class="gradeX odd2">
							<td colspan="7"></td>
							<td align="right" style="border-left: 1px solid #bbb;" >ttl cbm</td>
							<td id="totalCbm" align="right" style="border-right: 1px solid #bbb;">'.$totalCbm.'</td>
							<td align="right" style="border-right: 1px solid #bbb;">'.$totalQuantity.'</td>
							<td align="right"></td>
							<td align=center></td>
							<td align=right style="border: 1px solid #bbb;"></td>
							<td></td>
						</tr>';
	    if($curComm > 0)
	    {
		    $strTfoot .= '		<tr class="gradeX odd" style="height: 100px; border-top: 2px dotted #333 ;">
									<td colspan="9" style="vertical-align: middle;">comm commission B-Art '.$curComm.'%</td>                                 
									<td align="right" style="vertical-align: middle;">
			                                    	'.($curComm/100).'
									</td>
									<td align="right" style="vertical-align: middle;">
										'.$totalAmount.'
									</td>
			                                    <td align="center" style="vertical-align: middle;">
				                                    '.
			    										$strCurrency
				                                    .'
			                                    </td>
			                                    <td align="right" style="vertical-align: middle;">
													'.$totalBartTax.'
			                                    </td>
			                                    <td></td>
                                </tr>';
	    }
	    $strTfoot .= '          <tr class="gradeX odd1">
	                                	<td colspan="10">Amount Chargeable</td>
	                                	<td align="right">Total -</td>                            
	                                	<td align="center">
	                                		'.
	    										$strCurrency
		                                    .'
		                                </td>
	                                	<td class="total" align=right>
	                                		'.$totalAmountBeforeTax.'
	                                    </td>
	                                </tr>
	                                 <tr class="gradeX odd1">
	                                	<td colspan="9"></td>
	                                	<td>VAT</td>
	                                	<td style="text-align:right;">'.$curVAT.'%</td>                                	
	                                	<td>-</td>
	                                	<td align="right" style="border:1px solid #bbb ;">'.$totalAmountBeforeTax * ($curVAT/100).'</td>
	                                </tr>
	                                 <tr class="gradeX odd2">
	                                	<td colspan="10"></td>
	                                	<td align="right">Total</td>
	                                	<td align=center>	
	                                		'.
	    										$strCurrency
		                                    .'
	                                	</td>
	                                	<td align=right style="border:1px solid #bbb ;">
	                                		'.$totalAmountAfterTax.'
	                                    </td>
	                                </tr>
	                                                
	                                <input type="hidden" name="sum'.$strCurrency.'" value="'.$totalAmountAfterTax.'" >
	                                <input type="hidden" name="sum'.$strCurrency.'Fact" value="'.$totalAmountAfterTaxFact.'" >                 
						';
		$res['tfoot'] = $strTfoot;
	    echo json_encode($res);	
	}
	else 
	{
		echo $isExistArticle;
	}
}
?>